/*
 * Copyright 2009 Douglas Lockamy, dlockamy_at_gmail.com
 * All rights reserved. Distributed under the terms of the MIT License.
 */

/*
    userfs spec
    
    FS File Types:
    1 Directory
    2 Text File
    3 Binary File
    4 Symlink

    FS File Read Flags
    1 Request Type
    2 Return Array of File in Directory

*/

MAX_FILE_DEPTH = 100;

var userfs_vfsNode =  vfsList[vfsList.length] = new vfsNode();
//vfsList[vfsList.length] = userfs_vfsNode ;
userfs_vfsNode.Name = 'userfs';

var userfs;

if (!this.userfs) userfs = {};

userfs.version = '0.1';
userfs_vfsNode.item=userfs;

userfs.hostAddress;
userfs.hashKey;
userfs.userName;

userfs.mountPoint;

userfs.init = function()
{

};

userfs.mount = function(serverAddress,fs_type,mountPoint,user,pass,argv)
{
    userfs.hostAddress = serverAddress;
    userfs.userName = user;
    userfs.mountPoint = mountPoint;
    urlString = serverAddress+"/kernel.php?username="+user+"&pass="+pass;
    benix.ajaxObj.open("GET",urlString,false);
    benix.ajaxObj.send(null);
       
    if(benix.ajaxObj.responseText != 'FAIL')
    {
        userfs.hashKey = benix.ajaxObj.responseText;
        userfs.initPrefs(userfs.read("/etc/user.pref",3,null,null,null) );
        
        return 1; 
    }
    else
    {
        return -1;
    }
    return -1;
};

userfs.umount = function()
{

};

userfs.cleanPath = function(path)
{
    if(path[path.length-1]=="#")
    {
        var stringPath = path.split("#");
        return(stringPath[0]);
    }
    if(path[path.length-1]=="@")
    {
        var stringPath = path.split("@");
        return(stringPath[0]);
    }
    
    return -1;
};

userfs.read = function(path,flags)
{
    var parsedPath=0;
    var splitCount=0;
    var splitPath = path.split("/");

    urlString = serverAddress+"/kernel.php?username="+userfs.userName+"&hash="+userfs.hashKey+"&function_id=01"+"&function_flags="+flags+"&path="+path;
    benix.ajaxObj.open("GET",urlString,false);
    benix.ajaxObj.send(null);
       
    if(benix.ajaxObj.responseText != 'FAIL' && flags == 1)
    {
        var pText = userfs.parseReponse(benix.ajaxObj.responseText);
        var count = 0;
        var returnText = 0;
        var searchPath = benix.ajaxObj.responseText;
        var modPath = searchPath.split(",");

        while(pText[count] != modPath[modPath.length] && count < modPath.length-1)
        {
            returnText = pText[count];
            count++;
        }
        
        if(returnText == modPath[modPath.length-1] && returnText[returnText.length-1] == "@" || returnText=="")
        {
            return 1;
        }
        else
            return 0;
    }
    else if(benix.ajaxObj.responseText != 'FAIL' && flags == 2)
    {
        count = 0;
        dirList = benix.ajaxObj.responseText.split(",");
        aNode = new fileNode();
            
        aNode.Name="/";
        aNode.Type = 1;
        aNode.prev = 0;
        aNode.next = 0;
        aNode.Data = new Array();
        aNode.Parent = 0;
    
        aNode.Data = new Array();
        
        while(count < dirList.length-1 )
        {
            aNode.Data[count] = new fileNode();
            aNode.Data[count].Name = userfs.cleanPath(dirList[count]);
            aNode.Data[count].Type = 1;
            aNode.Data[count].Data=0;
            aNode.Data[count].prev=aNode.Data[count-1];
            if(count > 0)
            {
                aNode.Data[count].prev=aNode.Data[count-1];            
                aNode.Data[count-1].next=aNode.Data[count];
            }
            else
                aNode.Data[count].prev=0;
            aNode.Data[count].next=0; 
            aNode.Data[count].Parent = aNode;
            
            count++;
        }
        return aNode;
    }
    else if(benix.ajaxObj.responseText != 'FAIL' && flags == 3)
    {
        return benix.ajaxObj.responseText;
    }
     else if(benix.ajaxObj.responseText != 'FAIL' && flags == 4)
    {
        return urlString;
    }   
    
    else
        return -10;
};

userfs.write = function(path,flags,buffer)
{
    var parsedPath=0;
    var splitCount=0;
    var splitPath = path.split("/");
 
    if(benix.ajaxObj.responseText != 'FAIL' && flags == 0)
    {
        urlString = serverAddress+"/kernel.php?username="+userfs.userName+"&hash="+userfs.hashKey+"&function_id=02"+"&function_flags="+flags+"&path="+path;
        benix.ajaxObj.open("GET",urlString,false);
        benix.ajaxObj.send(null);
        return benix.ajaxObj.responsText;
    }
    if(benix.ajaxObj.responseText != 'FAIL' && flags == 1)
    {
        urlString = serverAddress+"/kernel.php?username="+userfs.userName+"&hash="+userfs.hashKey+"&function_id=02"+"&function_flags="+flags+"&path="+path;
        benix.ajaxObj.open("GET",urlString,false);
        benix.ajaxObj.send(null);
        return benix.ajaxObj.responsText;
    }
    else if(benix.ajaxObj.responseText != 'FAIL' && flags == 2)
    {
        urlString = serverAddress+"/kernel.php?username="+userfs.userName+"&hash="+userfs.hashKey+"&function_id=02"+"&function_flags="+flags+"&path="+path;
        benix.ajaxObj.open("GET",urlString,false);
        benix.ajaxObj.send(null);
        return benix.ajaxObj.responsText;      
        return 0;
    }
    else if(benix.ajaxObj.responseText != 'FAIL' && flags == 3)
    {
        //alert("create link");
        return -1;
    }
    else
        return -10;

};

userfs.walk= function(aPath,sessionKey)
{
    urlString = serverAddress+"/kernel.php?username="+userfs.userName+"&hash="+userfs.hashKey+"&function_id=01"+"&function_flags="+"2"+"&path="+aPath;
    benix.ajaxObj.open("GET",urlString,false);
    benix.ajaxObj.send(null);
       
    if(benix.ajaxObj.responseText != 'FAIL' )
    {
        count = 0;
        dirList = benix.ajaxObj.responseText.split(",");
        aNode = new fileNode();
            
        aNode.Name=aPath;
        aNode.Type = 1;
        aNode.prev = 0;
        aNode.next = 0;
        aNode.Data = new Array();
        aNode.Parent = 0;
 /*   
        aNode.Data = new Array();
        
        while(count < dirList.length )
        {
            aNode.Data[count] = new fileNode();
            aNode.Data[count].Name = dirList[count-1];
            aNode.Data[count].Type = 1;
            aNode.Data[count].Data=0;
            aNode.Data[count].prev=aNode.Data[count-1];
            if(count > 0)
            {
                aNode.Data[count].prev=aNode.Data[count-1];            
                aNode.Data[count-1].next=aNode.Data[count];
            }
            else
                aNode.Data[count].prev=0;
            aNode.Data[count].next=0; 
            aNode.Data[count].Parent = aNode;
            
            count++;
        }*/
        return aNode;
    }
};

userfs.parseReponse = function(parseText)
{
    var pText = parseText;
    return( pText.split(",") );
};

userfs.initPrefs = function(prefList)
{
    var prefArray = prefList.split(";");
    var count = 0;
    
    while(count < prefArray.length-1 )
    {
        var prefItem = prefArray[count].split("=");
        
        if(prefItem[0] == "AutoResume" && prefItem[1]=="Yes")
            benix.fsCopy(userfs.mountPoint+"/dev/window/*","/local/window","0r");
        if(prefItem[0]=="LinkEtc" && prefItem[1]=="Yes")
            benix.write(userfs.mountPoint+"/etc",4,"/etc",null,null,null);
        count++;
    };
    return 0;
};

